#include <stdio.h>
#include <dlfcn.h>

int main(int argc, char *argv[])
{
	void *handle;
	void (*display)(void);
	int (*addi32)(int a, int b);
	float (*addf32)(float a, float b);
	
	//打开动态库
	handle = dlopen("./libmath.so", RTLD_NOW);
	if(!handle)
	{
		printf("dlopen:%s\n", dlerror());
		return -1;
	}
	
	//从动态库中查找函数
	display = dlsym(handle, "display");
	if(display)
		(*display)();
	else
		printf("find display failed;%s\n", dlerror());
		
	addi32 = dlsym(handle, "addi32");
	if(addi32)
		printf("%d\r\n", (addi32)(20, 30));
	else
		printf("find addi32 failed;%s\n", dlerror());
	addf32 = dlsym(handle, "addf32");
	if(addf32)
		printf("%f\r\n", (*addf32)(20, 30));
	else
		printf("find addf32 failed;%s\n", dlerror());
	
	//关闭动态库
	dlclose(handle);
	return 0;
}
